package com.jt.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;

@Service(timeout = 3000)
public class DubboOrderServiceImpl implements DubboOrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	
	/**
	 * 订单入库
	 */
	@Transactional
	@Override
	public String saveOrder(Order order) {
		//1.生成orderId
		String orderId=""+order.getUserId()+System.currentTimeMillis();
		Date date=new Date();
		//2.订单入库
		order.setStatus(1)
			 .setOrderId(orderId)
			 .setCreated(date)
			 .setUpdated(date);
		orderMapper.insert(order);
		System.out.println("订单信息入库成功！");
		//3.订单物流入库
		OrderShipping orderShipping = order.getOrderShipping();
		orderShipping.setOrderId(orderId)
					 .setCreated(date)
					 .setUpdated(date);
		orderShippingMapper.insert(orderShipping);
		System.out.println("订单物流信息入库成功！");
		//4.订单商品入库
		List<OrderItem> orderItems = order.getOrderItems();
		for (OrderItem orderItem : orderItems) {
			orderItem.setOrderId(orderId)
					 .setCreated(date)
					 .setUpdated(date);
			orderItemMapper.insert(orderItem);
		}
		System.out.println("订单商品信息入库成功！");
		return orderId;
	}

	/**
	 * 利用id查询订单的全部信息
	 */
	@Override
	public Order findOrderById(String id) {
//		Order order = orderMapper.selectById(id);
//		OrderShipping orderShipping = orderShippingMapper.selectById(id);
//		QueryWrapper<OrderItem> queryWrapper=new QueryWrapper<>();
//		queryWrapper.eq("order_id", id);
//		List<OrderItem> list = orderItemMapper.selectList(queryWrapper);
//		order.setOrderShipping(orderShipping)
//			 .setOrderItems(list);
//		return order;
		//思考:
		//1.使用3个mapper查询数据库导致性能降低.最好的做法能否将sql一起运行.
		//2.尽可能使用单表查询.
		//实现方式:
		//利用mybatis实现上述操作,同时完成数据的封装.
		Order order = orderMapper.findOrderById(id);
		return order;
	}
}
